package com.travel.server.manager.impl.controller.sms;

import com.alibaba.fastjson.JSONObject;
import com.travel.server.manager.dto.request.EmptyRequest;
import com.travel.server.manager.dto.request.message.ReceiveResultRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.dal.MessageDal;
import com.travel.server.manager.model.MessageTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/28 19:00
 * @Desc:
 */
@RestController
@Slf4j
public class ReceiveController {
    @Autowired
    private MessageDal messageDal;

    @RequestMapping(value = "/receive_result")
    public EmptyResponse receive(@RequestBody ReceiveResultRequest request){
        EmptyResponse response = new EmptyResponse();
        try {
            log.info("云之讯短信审核状态通知, {}", JSONObject.toJSONString(request));

            if (StringUtils.isBlank(request.getTemplateId())) {
                return response;
            }
            MessageTemplate messageTemplate = messageDal.selectByTemplateId(request.getTemplateId());
            if (messageTemplate == null) {
                return response;
            }
            String codeStr = request.getCode();
            Integer code = Integer.parseInt(codeStr);
            messageTemplate.setExamState(code);
            messageTemplate.setUpdateTime(new Date());
            messageDal.inserOrUpdate(messageTemplate);
        }catch (Exception e){
            log.error("云之讯短信审核结果接收处理异常：", e);
        }
        return response;
    }
}
